package com.googlecode.jsimplemarket.service;

import com.googlecode.jsimplemarket.repository.AbsentaRepository; 

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service(value = "absentaService")
public class AbsentaService {
	@Autowired
	private AbsentaRepository absentaRepository;
	
	
	private Date getDataInceputZi(Date data) {
        GregorianCalendar gregorianCalendar = new GregorianCalendar();
        gregorianCalendar.setTime(data);
        gregorianCalendar.set(GregorianCalendar.HOUR, 0);
        gregorianCalendar.set(GregorianCalendar.MINUTE, 0);
        gregorianCalendar.set(GregorianCalendar.SECOND, 0);
        gregorianCalendar.set(GregorianCalendar.AM_PM, GregorianCalendar.AM);
        return gregorianCalendar.getTime();
    }
    
    private Date getDataSfarsitZi(Date data) {
        GregorianCalendar gregorianCalendar = new GregorianCalendar();
        gregorianCalendar.setTime(data);
        gregorianCalendar.set(GregorianCalendar.AM_PM, GregorianCalendar.PM);
        gregorianCalendar.set(GregorianCalendar.HOUR, 11);
        gregorianCalendar.set(GregorianCalendar.MINUTE, 59);
        gregorianCalendar.set(GregorianCalendar.SECOND, 59);
        return gregorianCalendar.getTime();
    }
    
    private GregorianCalendar getTodayCalendar() {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.set(GregorianCalendar.HOUR_OF_DAY, 0);
        calendar.set(GregorianCalendar.MINUTE, 0);
        calendar.set(GregorianCalendar.SECOND, 0);
        //calendar.set(GregorianCalendar.AM_PM, GregorianCalendar.AM);
        calendar.set(GregorianCalendar.MILLISECOND, 0);
        return calendar;
    }
    
    private Date getTodayDate() {
        return getTodayCalendar().getTime();
    }
    
    private Date addDays(Date date, int days) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.add(Calendar.DATE, days);
                 
        return cal.getTime();
    }
    
    private Date get7DayDate() {
        GregorianCalendar calendar = getTodayCalendar();
        calendar.add(GregorianCalendar.DATE, -20);
        
        return calendar.getTime();
    }
    
    private Date getLastMonthDate() {
        GregorianCalendar calendar = getTodayCalendar();
        calendar.set(GregorianCalendar.DATE, 1);
        
        return calendar.getTime();
    }
	
	
    public Object[] queryBySpatiuLastMonth(Long idSpatiu, Long idFirma) {
        Date day30 = getLastMonthDate();
        /* List<Object[]> vanzari = absentaRepository.queryBySpatiu(idSpatiu, idFirma, day30); */
        //return absentaRepository.queryBySpatiu(1L, 1L, day30);
        
        List<Object[]> objs = absentaRepository.queryBySpatiu(idSpatiu, idFirma, day30);
        if (objs.isEmpty()) {
                return null;
            } else {
                return objs.get(0);
            }
    }
    
    public List<Object[]> queryByDay(Date data) {
        Date dataStart = getDataInceputZi(data);
        Date dataSfarsit = getDataSfarsitZi(data);
        return absentaRepository.queryByDay(dataStart);
    }
    public List<Object[]> queryByDayAll(Date data) {
        Date dataStart = getDataInceputZi(data);
        Date dataSfarsit = getDataSfarsitZi(data);
        return absentaRepository.queryByDayAll(dataStart);
    }
    
    public List<Object[]> queryByTaraba(Long idSpatiu) {        
        return absentaRepository.queryByTaraba(idSpatiu);
    }
    
    public Long AbsenteByDay(Date data){
    	
    	Date dataStart = getDataInceputZi(data);
    	List<Long> totalAbs = absentaRepository.queryByDayTotal(dataStart);
    	
    	if (totalAbs.isEmpty()) {
            return 0L;
        } else {
            return totalAbs.get(0);
        }
    	
    }

}
